package com.cupk.wudi.practice.apidemo.controller;

import com.cupk.wudi.practice.apidemo.entity.Animal;
import com.cupk.wudi.practice.apidemo.service.AnimalService;
import com.cupk.wudi.practice.apidemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RestController
public class AnimalController {

    @Autowired
    AnimalService animalService;

//    @GetMapping("/animal/{type}")
//    List<Animal> getAnimalByType(@PathVariable("type") String type){
//        return animalService.getByType(type);
//
//
//    }
//    @GetMapping("/animal/{name}")    //写法一：简洁
//    Result<Animal> getAnimalByName(@PathVariable String name){
//        Animal animal=animalService.getByName(name);
//        return Result.success(animal);
//    }

    @GetMapping("/animal")   //写法二：Param写法，直观
    Result<Animal> getAnimalByName(@RequestParam String name){
        Animal animal=animalService.getByName(name);
        return Result.success(animal);
    }

    @PostMapping("/animal/add")
    Result<String> addAnimal(@RequestBody Animal animal){   //数据库中添加动物
        if(animalService.addAnimal(animal)>0){
            return Result.success("成功！！");
        }
        else {
            return Result.error("失败！！");
        }
    }

    @DeleteMapping("/animal/del")
    Result<String> deleteAnimal(@RequestParam String IDs){   // 按照编号批量删除
        if(animalService.deleteAnimalByIDs(IDs)>0){
            return Result.success("成功删除数据");
        }
        else{
            return Result.error("删除失败");
        }
    }

    @PutMapping("/animal/update")
    Result<String> updateAnimal(@RequestBody Animal animal){
        if(animalService.updateAnimal(animal)>0){
            return Result.success("数据更新成功");
        }
        else{
            return Result.error("数据更新失败");
        }
    }
}